<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <operation-record-search @search="reload" />
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :scroll="{ x: 1000 }"
        cache-key="proSystemOperationRecordTable"
      >
        <!-- <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a @click="openDetail(record)">删除</a>
          </template>
        </template> -->
      </ele-pro-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro/es';
  import OperationRecordSearch from './components/operation-record-search.vue';
  import { pageOperationRecords } from '@/api/settings/operation-record';
  import type { OperationRecordSearchParams } from '@/api/settings/operation-record/model';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '账号',
      dataIndex: 'account',
      sorter: true,
      showSorterTooltip: false,
      width: 200,
      ellipsis: true
    },
    {
      title: '请求地址',
      dataIndex: 'ip',
      align: 'center'
    },

    {
      title: '操作时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text),
      align: 'center'
    },
    {
      title: '操作说明',
      dataIndex: 'remark',
      width: 400,
      align: 'center'
    }
  ]);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    return pageOperationRecords({
      ...where,
      ...orders,
      ...filters,
      index: page,
      size: limit
    });
  };

  /* 刷新表格 */
  const reload = (where?: OperationRecordSearchParams) => {
    tableRef?.value?.reload({ page: 1, where });
  };
</script>
